import {
  ArgumentsHost,
  BadRequestException,
  Catch,
  ExceptionFilter,
  Inject,
} from '@nestjs/common';
import { Response } from 'express';
import { Logger } from 'nestjs-pino';

@Catch(BadRequestException)
export class BadRequestFilter implements ExceptionFilter {
  constructor(@Inject(Logger) private logger: Logger) {}
  catch(exception: BadRequestException, host: ArgumentsHost) {
    console.log('badrequest-execption.filter');
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    // const request = ctx.getRequest<Request>();

    // 返回标准化错误响应
    response.status(500).json({
      error: exception,
    });
  }
}
